package by.grsu.pochebut.webpage.booking;


import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import by.grsu.pochebut.dao.DaoAdmin;
import by.grsu.pochebut.entity.Admin;
import by.grsu.pochebut.entity.Booking;
import by.grsu.pochebut.entity.Car;
import by.grsu.pochebut.entity.Client;
import by.grsu.pochebut.interf.InterfAdmin;
import by.grsu.pochebut.interf.InterfBooking;
import by.grsu.pochebut.webpage.MyPage;

public class EditBookingPage extends MyPage {

	@SpringBean
	private InterfBooking daoBook;

	@SuppressWarnings("serial")
	public EditBookingPage(final Booking bookToEdit) {
		super();

	
		// create form fields and set data
		final TextField<Client> clientField = new TextField<Client>("client", new Model<Client>(bookToEdit.getClient()));
		final TextField<Car> carField=new TextField<Car>("car",new Model<Car>(bookToEdit.getCar()));
		final TextField<Double> billField=new TextField<Double>("bill",new Model<Double>(bookToEdit.getBill()));
		clientField.setType(Client.class);
		carField.setType(Car.class);
		billField.setType(Double.class);
		
		
		
		
		// create "form" component which is required for data retrieving from
		// client to server
		Form<Booking> form = new Form<Booking>("form", new Model<Booking>(bookToEdit)) {
			@Override
			public void onSubmit() {
				
				// retrieve data from the form and set it to the object
				bookToEdit.setClient(clientField.getModelObject());
				bookToEdit.setCar(carField.getModelObject());
				bookToEdit.setBill(billField.getModelObject());
				
				// save updated object in DB
				daoBook.save(bookToEdit);
				// go to the list page
				setResponsePage(ListBookingPage.class);
				
			}
		};

		// add form fields
		form.add(clientField);
		form.add(carField);
		form.add(billField);

		// add buttons
		form.add(new Button("saveLink"));
		form.add(new Link<Booking>("cancelLink") {
			private static final long serialVersionUID = -1521748836027504873L;

			@Override
			public void onClick() {
				setResponsePage(ListBookingPage.class);
			}
		});
		// add form to the page
		add(form);

		// add special component which will allow to display "validation errors"
		// and feedback messages
		FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
		add(feedbackPanel);
	}

}
